<?php
// $Id$

/**
 * Controller_Admin_Userpackage 控制器
 */
class Controller_Admin_Userpackage extends Controller_Abstract
{
	function actionuserinfoadd()
	{
// 		$this->union_table = Userpackage::meta();
// 		//ini_set('allow_call_time_pass_reference', true); //开启引用？
		$package = Acctpackage::find('agent_id = ?',$_SESSION['acct']['id'])->setColumns('id,packagename')->asArray()->getAll();
		$package = Helper_Array::toHashmap($package, 'id','packagename');
		$id = intval($this->_context->id);
		$userinfo = Userpackage::find('id = ? and agent_id = ?',$id,$_SESSION['acct']['id'])->getOne();
		if (!$userinfo->id())
		{
			exit('没有找到数据');
		}
// 		$handle = new helper_autoinsert($this->union_table);
// 		$form = new Form_userperiodrate_userinfoadd(url($this->_context->controller_name.'/'.$this->_context->action_name));
// 		$result = $handle->add($form, 'id',array('agent_id'=>$_SESSION['acct']['id']));
	
// 		if ($this->_context->isGET())
// 		{
// 			$form->element('packageid')->items = $package;
// 			$form->element('begintime')->value =  date('Y-m-d H:i:s',$form->element('begintime')->value);
// 			$form->element('endtime')->value =  date('Y-m-d H:i:s',$form->element('endtime')->value);
				
				
// 			$this->_view['form'] = $result;
// 			$this->_viewname = '../form/add';
// 		}else {
// 			Syslog::logs('编辑用户套餐:'.$_POST['userid']);
// 			return call_user_func_array(array($this,$handle->status),array($result));
// 		}
		if($this->_context->isPOST())
		{
			$packageid = intval($this->_context->packageid);
			$starttime = strtotime($this->_context->begintime);
			$endtime = strtotime($this->_context->endtime);
			$minte = intval($this->_context->minte);
			$userinfo->packageid = $packageid;
			$userinfo->begintime = $starttime;
			$userinfo->endtime = $endtime;
			$userinfo->minte = $minte;
			$userinfo->save();
			return $this->success('修改成功');
			Syslog::logs("修改用户套餐，ID：".$this->_context->id);
		}else{
			
			$this->_view['userinfo'] = $userinfo;
		}
		$this->_view['package'] = $package;
	}
	function actionuserinfodel()
	{
		Syslog::logs("删除用户套餐，ID：".$this->_context->ids);
		$this->union_table = Userpackage::meta();
		$this->delete(array_merge(array('id'=>$this->_context->ids),array('agent_id'=>$_SESSION['acct']['id'])));
	}
	function _format_appendSql()
	{
		if (isset($_SESSION['acct']))
		{
			if ($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1)
			{
			//为管理
				$append['agent_id'][] = $_SESSION['acct']['id'];
				//$append['']
				//$append['admin_id'] = $_SESSION['acct']['id'];
				//$append= array();
			}else
			{
				//$append['admin_id'] = $_SESSION['acct']['admin_id'];
				//$append['agent_id'] = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
				$append['agent_id'][] = $_SESSION['acct']['id'];
			}
		}
		return $append;
	}
	function actionIndex()
	{
        $this->union_table = Acctpackage::meta();
     
        $this->lists($this->_format_appendSql());
	}
	
	function actionadd()
	{
		$this->union_table = Acctpackage::meta();
		//ini_set('allow_call_time_pass_reference', true); //开启引用？
		$handle = new helper_autoinsert($this->union_table);
		$form = new Form_ratepackage_add(url($this->_context->controller_name.'/'.$this->_context->action_name));
		$result = $handle->add($form, 'id',array('admin_id'=>$_SESSION['acct']['admin_id'],'agent_id'=>$_SESSION['acct']['id']));
		
		if ($this->_context->isGET())
		{
			$this->_view['form'] = $result;
			$this->_viewname = '../form/add';
		}else {
			Syslog::logs('编辑套餐:'.$this->_context->packagename);
			return call_user_func_array(array($this,$handle->status),array($result));
		}
	}
	function actiondel()
	{
		Syslog::logs("删除用户套餐配置，ID：".$this->_context->ids);
		$this->union_table = Acctpackage::meta();
		$this->delete(array_merge(array('id'=>$this->_context->ids),array('agent_id'=>$_SESSION['acct']['id'])));
		//return $this->success('删除成功');
	}
	function actionuserinfo()
	{
		if ($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1)
		{
			//为管理
			//$append['']
			$append['admin_id'] = $_SESSION['acct']['id'];
		
		}else
		{
			$append['admin_id'] = $_SESSION['acct']['admin_id'];
			//$append['agent_id'] = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
			//if (count($append['agent_id']) == 0)
			//{
			$append['agent_id'] = array($_SESSION['acct']['id']);
			//}
		}
		$this->union_table = Userpackage::meta();
		 
		$this->lists($append);
	}
	function actionlog()
	{
		if ($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1)
		{
			//为管理
			//$append['']
			$append['admin_id'] = $_SESSION['acct']['id'];
		
		}else
		{
			$append['admin_id'] = $_SESSION['acct']['admin_id'];
			$append['agent_id'] = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
			//if (count($append['agent_id']) == 0)
			//{
				$append['agent_id'] = array($_SESSION['acct']['id']);
			//}
		}
		$this->union_table = Userpackagelog::meta();
		$this->lists($append);
	}
	
}


